<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
class Content extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {   
        $CateId =input('cid');
        $modelId=input('model_id');
        if ($CateId) {
           $map=['a.cid'=>$CateId];
        }else{
            $map=[];
        }
        $artRes=db('archives')
                ->alias('a')
                ->join('cate c','c.id=a.cid')
                ->join('models m','m.id=a.model_id')
                ->field('a.*,c.catename,m.model_name')
                ->where($map)
                ->paginate(15,false,['query' => ['cid' => $CateId]]);
        $this->assign([
            'artRes'=>$artRes,
            'CateId'=>$CateId,
        ]);
       return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
       
        $cate=model('Cate')->cateSort();
        $model=model('Models')->field('id,model_name')->order('id desc')->select();
        $cid=input('cid');
        $this->assign([
            'cate'   =>$cate,
            'model'  =>$model,
            'cid'    =>$cid,
        ]);
        return view();
    }
    /**
     * 获取当前模型所有字段
     *
     * @return void
     */
    public function archs(){
        if (request()->isAjax()) {
            $cid =input('cid');
            $modelId=db('cate')->where('id',$cid)->field('model_id')->find();
            $futables=db('fields')->field('ename,type,cname,values')->where('model_id',$modelId['model_id'])->select();//附加表字段
            foreach ($futables as $k => $v) {
                if (!empty($v['values'])) {
                    $futables[$k]['values']=explode(',',$v['values']);
                }
            }
            return json_encode($futables);
        }
    }
    
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (request()->isPost()) {
            $data=$request->param(); 
            $modelId=db('cate')->where('id',$data['cid'])->field('model_id')->value('model_id');
            $data['model_id']=$modelId;//所属模型id
            foreach ($data as $k => $v) {
                    if (is_array($v)) {
                       $v=implode(',',$v);
                    }
                    $data[$k]=$v;
            }
            $data['pubdate']=time();
            $validate = validate('Content');
            if(!$validate->scene('save')->check($data)){
                 $this->error($validate->getError());
            }
           $res= model('Content')->allowField(true)->save( $data);
           if ($res) {
              $this->success('文档添加成功','index');
           }
           $this->error('文档添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $art=db('archives')->find($id);
        $cate=model('Cate')->cateSort();
        if ($art['flag']) {
            $art['flag']=explode(',',$art['flag']);
        }
        $tableName=db('models')->where('id',$art['model_id'])->value('table_name');//附加表名
        $diyData=db($tableName)->where('aid',$id)->find();//附加表内容
        $cloumns=array();//附加表所有字段
        $_tablename=config('database.prefix').$tableName;
        $_columns=Db::query("SHOW COLUMNS FROM {$_tablename}");
        foreach ($_columns as $v) {
             $cloumns[]=$v['Field'];
        }
        $fields=db('fields')->field('ename,type,cname,values')->where('model_id',$art['model_id'])->select();
        $this->assign([
            'art' =>$art,
            'cate'=>$cate,
            'fields'=>$fields,
            'diyData'=>$diyData,
        ]);
        return view();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request,$id)
    {
        if (request()->isPost()) {
            $data=input('post.');
            if ($data['flag']) {
                $data['flag']=implode(',',$data['flag']);
            }
            if ($data['filmtype']) {
                $data['filmtype']=implode(',',$data['filmtype']);
            }
            $validate = validate('Content');
            if(!$validate->scene('edit')->check($data)){
                 $this->error($validate->getError());
            }
            $res=model('Content')->save($data,['id' => $id]);
            if ($res) {
                $this->success('文档编辑成功','index');
            }
            $this->error('文档编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (request()->isAjax()) {
            $res=model('Content')->destroy($id);
            if ($res) {
               return json(['code'=>1,'msg'=>'文档删除成功']);
            }
            return json(['code'=>2,'msg'=>'文档删除失败']);
        }
    }
    
}
